package payment.simulator.institution.tx.contractsign;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import payment.api.tx.contractsign.Tx2721Request;
import cpcn.institution.tools.util.StringUtil;

/**
 * <pre>
 * Modify Information: 短信发送（客服系统专用）
 * Author       Date          Description
 * ============ ============= ============================
 * caohc        2018年11月30日       Create this file
 * </pre>
 */
public class Tx2721 extends HttpServlet {
	
	private static final long serialVersionUID = -1114166584340483329L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	try {
    		// 1.取得参数
	    	String institutionID = request.getParameter("InstitutionID");
	        String txSN = request.getParameter("TxSN");
	        String phoneNumber = request.getParameter("PhoneNumber");
	        String templateID = request.getParameter("TemplateID");
	        String[] values = request.getParameterValues("Value");
	        
	        
	        // 2.创建交易请求对象
	        Tx2721Request tx2721Request = new Tx2721Request();
	        tx2721Request.setInstitutionID(institutionID);
	        tx2721Request.setTxSN(txSN);
	        tx2721Request.setPhoneNumber(phoneNumber);
	        tx2721Request.setTemplateID(templateID);
	        if(values != null && values.length > 0){
                List<String> valueList = new ArrayList<String>(); 
                for(int i=0; i<values.length; i++){
                    if(StringUtil.isNotEmpty(values[i])){
                    	valueList.add(values[i]);
                    }
                }
                tx2721Request.setValueList(valueList);
            }
	
	        // 3.执行报文处理
	        tx2721Request.process();
	
	        // 4.将参数放置到request对象
	        // //3个交易参数
	        request.setAttribute("plainText", tx2721Request.getRequestPlainText());
	        request.setAttribute("message", tx2721Request.getRequestMessage());
	        request.setAttribute("signature", tx2721Request.getRequestSignature());
	        request.setAttribute("txCode", "2721");
	        request.setAttribute("txName", "短信发送（客服系统专用）");
	
	            // 1个action(支付平台地址)参数
	            request.setAttribute("action", request.getContextPath() + "/SendMessage");
	
	            // 5.转向Request.jsp页面
	            request.getRequestDispatcher("/Request.jsp").forward(request, response);
    	} catch (Exception e) {
            e.printStackTrace();
            processException(request, response, e.getMessage());
        }
    }


    private void processException(HttpServletRequest request, HttpServletResponse response, String errorMessage) {
    	try {
    		request.setAttribute("errorMessage", errorMessage);
    		request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
    		} catch (Exception e) {
    	}
    }
}
    